﻿#include "osm_frame_widget.h"
#include <QApplication>
#include <QLibraryInfo>
#include <QTranslator>
#include <QMap>
#include <QVariantMap>
#include "listen_thread.h"
#include "interface_utils.h"
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);

	//Options
	bool stdio = true;
	for (int i=1;i<argc;++i)
	{
		if (strstr(argv[i],"-stdio=off"))
			stdio = false;
	}


	QTranslator qtTranslator;
#if QT_VERSION>=0x060000
	if (qtTranslator.load("qt_" + QLocale::system().name(),
					  QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
		app.installTranslator(&qtTranslator);
#else
	if (qtTranslator.load("qt_" + QLocale::system().name(),
					  QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
		app.installTranslator(&qtTranslator);
#endif


	QTranslator appTranslator;
	QString strTransLocalFile =
			QCoreApplication::applicationDirPath()+"/" +
			QCoreApplication::applicationName()+"_"+
			QLocale::system().name()+".qm";
	bool loadRes = false;
	if (false==appTranslator.load(strTransLocalFile ))
		loadRes = true;
	else if (false==app.installTranslator(&appTranslator))
		loadRes = true;

	if (loadRes)
	{
		strTransLocalFile =
				":/" +
				QCoreApplication::applicationName()+"_"+
				QLocale::system().name()+".qm";
		if (appTranslator.load(strTransLocalFile ))
			app.installTranslator(&appTranslator);
	}

	osm_frame_widget w;
	w.show();


	//read cmd from stdin
	listen_thread * thread = new listen_thread(0);

	thread->connect(thread,&listen_thread::newCmd,&w,
					&osm_frame_widget::slot_call_function,
			Qt::QueuedConnection);
	thread->start();

	w.show_stdio_checkbox(stdio);
	w.turn_on_stdio(stdio);

	w.viewer()->listen_event("MainFrame",
							 [&](const QMap<QString,QVariant> mp)->void

	{
		std::string strCmd = map_to_string(mp).toStdString()+"\n";
		if (w.stdio_is_on())
		{
			fputs(strCmd.c_str(),stdout);
			fflush(stdout);
		}
	});


	int ret = app.exec();

	thread->terminate();
	thread->deleteLater();

	return ret;
}
